home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8171 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  143 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.dcs.warwick.ac.uk!not-for-mail
  3. From: D.C.Molero@dcs.warwick.ac.uk (Daniel Castillo Molero)
  4. Subject: Re: Permutations
  5. X-Nntp-Posting-Host: cray
  6. Message-ID: <1996Mar2.034112.3869@dcs.warwick.ac.uk>
  7. Sender: news@dcs.warwick.ac.uk (Network News)
  8. Organization: Department of Computer Science, Warwick University, England
  9. References: <Dn8yz9.GGy@spuddy.mew.co.uk> <4grvqb$8n9@hpbblb.bbn.hp.com>
  10. Date: Sat, 2 Mar 1996 03:41:12 GMT
  11.  
  12. In article <4grvqb$8n9@hpbblb.bbn.hp.com> Matthias Dittrich <matti> writes:
  13. >david@spuddy.mew.co.uk (David Turner) wrote:
  14. >>Does anyone know of an algorithm that works out the different purmutations
  15. >>of ordering a number of objects? Not calculating how many there are, but 
  16. >>actaully working out (quickly?) what they permutations are!
  17. >> 
  18. >>        eg. permutations of objects A, B and C:
  19. >> 
  20. >>                A B C   B A C   C A B
  21. >>                A C B   B C A   C B A
  22. >> 
  23. >>Any help would be greatly appreciated.
  24. >> 
  25. >>David
  26. >> 
  27. >Here is one I've just written:
  28. >
  29. >#include <stdio.h>
  30. >#include <string.h>
  31. >
  32. >#define MAX_PERM 8
  33. >
  34. >void permfunc(unsigned long i, char* str);
  35. >void do_perm(char* str);
  36. >
  37. >int main(int argc, char** argv)
  38. >{
  39. >  unsigned long n = 2, i;
  40. >  char str[MAX_PERM];
  41. >
  42. >    /* do some essential checks */
  43. >  if(argc < 2) return 1;
  44. >
  45. >  sscanf(argv[1], "%lu", &n);
  46. >
  47. >  if(n >= MAX_PERM)
  48. >  {
  49. >    n = MAX_PERM - 1;
  50. >    fprintf(stderr, "number too big, set to %d\n", n);
  51. >  }
  52. >
  53. >    /* initialize the string to show permutations */
  54. >  for(i=0; i<n; i++)
  55. >  {
  56. >    str[i] = 'A' + i;
  57. >  }
  58. >  str[n] = '\0';
  59. >
  60. >    /* call permfunc with starting level 0 */
  61. >  permfunc(0, str);
  62. >
  63. >  return 0;
  64. >}
  65. >
  66. >void permfunc(unsigned long i, char* str)
  67. >{
  68. >  char s[MAX_PERM];
  69. >  char *t = s + i;
  70. >  char *u = str + i;
  71. >
  72. >    /* check if at least two characters are in the string */
  73. >  if(*u && *(u + 1))
  74. >  {
  75. >    strcpy(s, str);
  76. >
  77. >    permfunc(i + 1, s);
  78. >    u = t + 1;
  79. >
  80. >      /* the number of characters in the string is the number of
  81. >         permutations to do */
  82. >    while(*u)
  83. >    {
  84. >        /* after the permutation has been done
  85. >           call this function recursively, increasing the starting level */
  86. >      do_perm(t);
  87. >      permfunc(i + 1, s);
  88. >      u++;
  89. >    }
  90. >  }
  91. >  else
  92. >  {
  93. >    printf("%s\n", str);
  94. >  }
  95. >}
  96. >
  97. >  /* do the permutation on a string */
  98. >void do_perm(char* str)
  99. >{
  100. >  char *s, *t, c;
  101. >
  102. >  s = t = str;
  103. >  t++;
  104. >
  105. >    /* save first char */
  106. >  c = *s;
  107. >    /* move all chars one position to the left */
  108. >  while(*t) *s++ = *t++;
  109. >    /* set the last char */
  110. >  *s = c;
  111. >}
  112. >
  113. >I think there may be faster ways to do it, but it works.
  114. >
  115. >Good luck,
  116. >Matthias
  117. >
  118.  
  119.  
  120. I'm very interested in using this program to generate permutations, but when
  121. I compile it with 'gcc program.c'  and then run it I get a strange result.
  122. For example, if the executable is called permut, and I enter
  123.  
  124.    permut a b c d
  125.  
  126. I get as output 
  127.  
  128.    AB
  129.    BA
  130.  
  131. I am a beginner in c and would appreciate very much if you could tell me
  132. what I am doing wrong.
  133.  
  134. Thank you for reading this message
  135.  
  136. danny
  137.  
  138.  
  139. -- 
  140. * Daniel Castillo.  D.C.Molero@dcs.warwick.ac.uk *
  141.  
  142.  
  143.